﻿using System;
using System.Collections.Generic;

public class BattleFrog
{
    public double PossitionX { get; set; }
    public double PossitionY { get; set; }
    public int Cooldown { get; set; }
    public double DestinationX { get; set; }
    public double DestinationY { get; set; }
    public PlayerState MoveMode { get; set; }
    private const double FrogRadius = 3.5;
    private const double SafetyZone = 15;

    public BattleFrog(double possitionX, double possitionY, int cooldown)
    {
        this.PossitionX = possitionX;
        this.PossitionY = possitionY;
        this.Cooldown = cooldown;
        this.DestinationX = 0;
        this.DestinationY = 0;
        this.MoveMode = PlayerState.Attack;
    }

    public void Move(double possitionX, double possitionY)
    {
        Console.WriteLine("move {0} {1}", possitionX, possitionY);
    }

    public void Shoot(double possitionX, double possitionY)
    {
        Console.WriteLine("shoot {0} {1}", possitionX, possitionY);
    }

    public void EnterSurvivalMode(Bullet bullet)
    {
        var bulletTraecktory = FindPath(bullet.PossitionX, bullet.PossitionY, bullet.DestinationX, bullet.DestinationY, 30);

        var isInDanger = CheckIfInDanger(bulletTraecktory, this.PossitionX, this.PossitionY);

        if (isInDanger)
        {
            var x3 = bullet.PossitionX - this.PossitionX;
            var y3 = bullet.PossitionY - this.PossitionY;
            Point safePositionOne = new Point(this.PossitionX - y3, this.PossitionY + x3);
            Point safePositionTwo = new Point(this.PossitionX + y3, this.PossitionY - x3);
            var safeRouteOne = FindPath(safePositionOne.X, safePositionOne.Y, this.PossitionX, this.PossitionY, 5);
            var isSafe = CheckIfInDanger(safeRouteOne, safeRouteOne[1].X, safeRouteOne[1].Y);
            if (isSafe)
            {
                this.DestinationX = safePositionOne.X;
                this.DestinationY = safePositionOne.Y;
            }
            else
            {
                this.DestinationX = safePositionTwo.X;
                this.DestinationY = safePositionTwo.Y;
            }

            this.MoveMode = PlayerState.Dodge;
        }
    }

    private static List<Point> FindPath(double currentX, double currentY, double destinationX, double destinationY, int counter)
    {
        List<Point> pathTracker = new List<Point>();
        var xDeviation = Math.Abs(currentX - destinationX) / 10;
        var yDeviation = Math.Abs(currentY - destinationY) / 10;
        var differenceX = currentX;
        var differenceY = currentY;


        for (int i = 0; i < counter; i++)
        {
            if (currentX > destinationX)
            {
                differenceX -= xDeviation;
            }
            else
            {
                differenceX += xDeviation;
            }

            if (currentY > destinationY)
            {
                differenceY -= yDeviation;
            }
            else
            {
                differenceY += yDeviation;
            }

            pathTracker.Add(new Point(differenceX, differenceY));
        }
        return pathTracker;
    }

    private bool CheckIfInDanger(List<Point> bulletTraecktory, double possitionX, double possitionY)
    {
        var isInDanger = false;
        for (int i = 0; i < bulletTraecktory.Count; i++)
        {
            double enemyDistance = CalculateDistance(bulletTraecktory[i].X, bulletTraecktory[i].Y, PossitionX, PossitionY);

            if (enemyDistance < FrogRadius + SafetyZone)
            {
                isInDanger = true;
                break;
            }
        }
        return isInDanger;
    }

    private static double CalculateDistance(double firstX, double firstY, double secondX, double secondY)
    {
        var result = Math.Sqrt((firstX - secondX) * (firstX - secondX) + (firstY - secondY) * (firstY - secondY));
        return result;
    }
}

